package cz.muni.fi.pa165.ws;

import java.util.List;

import cz.muni.fi.pa165.sportmanager.sportactivityws.CreateSportActivityRequest;
import cz.muni.fi.pa165.sportmanager.sportactivityws.CreateSportActivityResponse;
import cz.muni.fi.pa165.sportmanager.sportactivityws.DeleteSportActivityRequest;
import cz.muni.fi.pa165.sportmanager.sportactivityws.DeleteSportActivityResponse;
import cz.muni.fi.pa165.sportmanager.sportactivityws.FindAllSportActivityRequest;
import cz.muni.fi.pa165.sportmanager.sportactivityws.FindAllSportActivityResponse;
import cz.muni.fi.pa165.sportmanager.sportactivityws.FindSportActivityRequest;
import cz.muni.fi.pa165.sportmanager.sportactivityws.FindSportActivityResponse;
import cz.muni.fi.pa165.sportmanager.sportactivityws.SportActivity;
import cz.muni.fi.pa165.sportmanager.sportactivityws.UpdateSportActivityRequest;
import cz.muni.fi.pa165.sportmanager.sportactivityws.UpdateSportActivityResponse;
import cz.muni.fi.pa165.ws.adapter.ISportActivityAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

/**
 * @author forman
 */
@Endpoint
public class SportActivityWSEndpoint {

	private static final String NAMESPACE = "http://muni.cz/fi/pa165/sportManager/sportActivityWS";

	@Autowired
	private ISportActivityAdapter sportActivityAdapter;

	@PayloadRoot(localPart = "CreateSportActivityRequest", namespace = NAMESPACE)
	@ResponsePayload
	CreateSportActivityResponse
	createSportActivity(@RequestPayload final CreateSportActivityRequest createSportActivityRequest) {
		sportActivityAdapter.createSportActivity(createSportActivityRequest.getSportActivities());
		return new CreateSportActivityResponse();
	}

	@PayloadRoot(localPart = "DeleteSportActivityRequest", namespace = NAMESPACE)
	@ResponsePayload
	DeleteSportActivityResponse
	deleteSportActivity(@RequestPayload final DeleteSportActivityRequest deleteSportActivityRequest) {
		sportActivityAdapter.deleteSportActivity(deleteSportActivityRequest.getSportActivities());
		return new DeleteSportActivityResponse();
	}

	@PayloadRoot(localPart = "UpdateSportActivityRequest", namespace = NAMESPACE)
	@ResponsePayload
	UpdateSportActivityResponse
	updateSportActivity(@RequestPayload final UpdateSportActivityRequest updateSportActivityRequest) {
		sportActivityAdapter.updateSportActivity(updateSportActivityRequest.getSportActivities());
		return new UpdateSportActivityResponse();
	}

	@PayloadRoot(localPart = "FindSportActivityRequest", namespace = NAMESPACE)
	@ResponsePayload
	FindSportActivityResponse
	findSportActivity(@RequestPayload final FindSportActivityRequest findSportActivityRequest) {
		final SportActivity sportActivity = sportActivityAdapter.findSportActivity(Long.valueOf(findSportActivityRequest.getSportActivityId()));
		final FindSportActivityResponse findSportActivityResponse = new FindSportActivityResponse();
		findSportActivityResponse.setSportActivity(sportActivity);
		return findSportActivityResponse;
	}

	@PayloadRoot(localPart = "FindAllSportActivityRequest", namespace = NAMESPACE)
	@ResponsePayload
	FindAllSportActivityResponse
	findAllSportActivity(@RequestPayload final FindAllSportActivityRequest findAllSportActivityRequest) {
		final List<SportActivity> allSportActivity = sportActivityAdapter.findAllSportActivity();
		final FindAllSportActivityResponse findAllSportActivityResponse = new FindAllSportActivityResponse();
		findAllSportActivityResponse.getSportActivities().addAll(allSportActivity);
		return findAllSportActivityResponse;
	}
}
